package com.Tradition.controller.business;

import com.Tradition.common.http.HttpResult;
import com.Tradition.common.http.HttpResultDataPage;
import com.Tradition.common.http.HttpResultEnum;
import com.Tradition.common.http.VerificationCodeEnum;
import com.Tradition.constant.SessionEnum;
import com.Tradition.controller.BaseController;
import com.Tradition.model.Bad;
import com.Tradition.model.User;
import com.Tradition.service.BadService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.servlet.http.HttpSession;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * ContentController
 *
 * @author 朱笑
 * @date 2018-06-25 15:16
 */
@Api(value="内容操作",tags={"内容的提交、编辑、删除"})
@RestController
@RequestMapping("bad")
public class BadController extends BaseController {
    @Autowired
    BadService badService;
    @ApiOperation(value="内容提交接口",notes="")
    public HttpResult submit(@ApiParam(name="区域编号",value="areaid",required=true)Integer areaid, @ApiParam(name="解释",value="annotation",required=true)String annotation, @ApiParam(name="内容",value="content",required=true)String content, @ApiParam(name="验证码(BAD_SUBMIT)",value="code",required=true)String code, HttpSession session){
        logger.info("请求参数："+areaid+","+annotation+","+content+","+code);
        if(code==null||!session.getAttribute(VerificationCodeEnum.BAD_SUBMIT.getName()).equals(code)){
            return HttpResult.newInstance(HttpResultEnum.VERIFICATIONCODE_ERROR);
        }
        if((areaid==null||areaid<=0)||(annotation==null||annotation.length()>200)|| (content==null||content.length()>100)){
            return HttpResult.newInstance(HttpResultEnum.PARAM_ABNORMAL);
        }
        User user= (User) session.getAttribute(SessionEnum.USER.getSessionKey());
        int result=badService.insertBad(areaid,annotation,content,user.getId());
        return result>0?HttpResult.newInstance(HttpResultEnum.SUCCESS):HttpResult.newInstance(HttpResultEnum.FAILURE);
    }
    @ApiOperation(value="内容查询接口",notes="根据单一id查询内容")
    @GetMapping("queryBad")
    public HttpResult queryBad(@ApiParam(name="内容id",value="badId",required=true)Integer badId){
        Bad bad=badService.queryBadById(badId);
        return HttpResult.newInstance(HttpResultEnum.SUCCESS).withData(bad);
    }
    @ApiOperation(value="内容查询接口",notes="分页查询")
    @GetMapping("queryBadPage")
    public HttpResult queryBadPage(@ApiParam(name="页码",value="pageNum",required=true)Integer pageNum,@ApiParam(name="每页行数",value="pageSize",required=true)Integer pageSize){
        List<Bad> bads = badService.queryBadPage(pageNum, pageSize);
        HttpResultDataPage page=new HttpResultDataPage(bads);
        return HttpResult.newInstance(HttpResultEnum.SUCCESS).withData(page);
    }
}
